package org.grow.took.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.took.dao.BookDao;
import org.grow.took.entity.*;
import org.grow.took.exception.SeptemberException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ceto/book")
public class BookController {
    @Autowired
    private BookDao bookDao;

    @PostMapping("/query")
    public PagedResults<Book> query(@RequestBody BookSearchForm bookSearchForm) {
        System.out.println(bookSearchForm);
        Integer pageNum = bookSearchForm.getPageNum();
//        if (pageNum<=0){
//            throw new RuntimeException();//下面的代码都不会运行
//        }
        if (pageNum <= 0) {
            throw new SeptemberException("抱歉，查看页数至少为一页");
        }
        Integer pageSize =bookSearchForm.getPageSize();
        BookQueryCondition queryCondition = bookSearchForm.getQueryCondition();
                ;
        IPage<Book> page = new Page(pageNum, pageSize);
        //playerDao.dynamicQuery(queryCondition);
        IPage<Book> bookIPage = bookDao.dynamicQuery(page,queryCondition);
        List<Book> records = bookIPage.getRecords();
        long total = bookIPage.getTotal();
        PagedResults<Book> pagedResults = new PagedResults();
        pagedResults.setResults(records);
        pagedResults.setTotal(total);
        return pagedResults;
        //return null;
    }


}
